package com.auto.learning.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.event.listener.DialogListener;
import com.auto.learning.event.listener.OnSubTipClickListener;
import com.auto.learning.tools.CommonMethod;
import com.auto.learning.tools.GBSSDialog;

public class CommonAlertDialog extends GBSSDialog implements android.view.View.OnClickListener{

	protected String tilte;
	protected String[] btnTexts;
	protected DialogListener listener;
	private OnSubTipClickListener onSubTipClickListener;

	View containView;
	View mainView;
	RelativeLayout mainViewLayout;
	LinearLayout buttonlayout,sub_tips_layout;
	int mainViewheight;
	public Button button1, button2, button3, buttonSub;
	TextView tv_dialog_title;
	ImageView dialog_close;
	protected float widthRatio = 0.8f;
	protected boolean isTextMessageTip = true;
	protected boolean showTitle = false;
	protected boolean showDialogClose = false;
	
	public void setShowDialogClose(boolean showDialogClose) {
		this.showDialogClose = showDialogClose;
	}

	private int dialogTextPosition = 0; // 0 zhong  1 left 
	
	
	private String subText;

	public void setSubText(String subText) {
		this.subText = subText;
	}


	public int getDialogTextPosition() {
		return dialogTextPosition;
	}

	public void setDialogTextPosition(int dialogTextPosition) {
		this.dialogTextPosition = dialogTextPosition;
	}

	public boolean isShowTitle() {
		return showTitle;
	}

	/**
	 * 是否显示title
	 * @param showTitle
	 */
	public void setShowTitle(boolean showTitle) {
		this.showTitle = showTitle;
	}


	public void setTextMessageTip(boolean isTextMessageTip) {
		this.isTextMessageTip = isTextMessageTip;
	}

	public float getWidthRatio() {
		return widthRatio;
	}

	/**
	 * 设置dialog宽度比例
	 * @param widthRatio
	 */
	public void setWidthRatio(float widthRatio) {
		this.widthRatio = widthRatio;
	}

	public CommonAlertDialog(Context context) {
		super(context, R.style.dialog);
		this.mContext = context;
		setCanceledOnTouchOutside(false);
		setCancelable(true);
	}
	
	public String getTilte() {
		return tilte;
	}
	/**
	 * 设置标题
	 * @param tilte
	 */
	public void setTilte(String tilte) {
		this.tilte = tilte;
	}

	public String[] getBtnTexts() {
		return btnTexts;
	}

	/**
	 * 设置下面的点击按钮数组，最大长度为3
	 * @param btnTexts
	 */
	public void setBtnTexts(String[] btnTexts) {
		this.btnTexts = btnTexts;
	}
	
	public CommonAlertDialog(Context _context, int theme, DialogListener _listener) {
		super(_context, theme);
		this.mContext = _context;
		listener = _listener;
	}

	public CommonAlertDialog(Context _context, int theme) {
		super(_context, theme);
		this.mContext = _context;
	}

	public void setAlertDialogListener(DialogListener _listener) {
		listener = _listener;
	}
	
	public void setOnSubTipClickListener(OnSubTipClickListener onSubTipClickListener) {
		this.onSubTipClickListener = onSubTipClickListener;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Activity activity = (Activity) mContext;
		WindowManager windowManager = activity.getWindowManager();
		containView = LayoutInflater.from(mContext).inflate(R.layout.dialog_alert_view, null);
		this.setContentView(containView);
		Window window = dialog.getWindow();
		WindowManager.LayoutParams _lp = window.getAttributes();
		Display display = windowManager.getDefaultDisplay();
		@SuppressWarnings("deprecation")
		int width = display.getWidth();
		_lp.width  = (int)(width * widthRatio);
		_lp.height = LayoutParams.WRAP_CONTENT;
		window.setAttributes(_lp);
		mainViewLayout = (RelativeLayout) findViewById(R.id.mainViewLayout);
		buttonlayout = (LinearLayout) findViewById(R.id.buttonlayout);
		sub_tips_layout = (LinearLayout) findViewById(R.id.sub_tips_layout);
		tv_dialog_title = (TextView) findViewById(R.id.tv_dialog_title);
		dialog_close = (ImageView) findViewById(R.id.dialog_close);
		FrameLayout fg_tip = (FrameLayout) findViewById(R.id.fg_tip);
		if (TextUtils.isEmpty(tilte)) {
			tilte = "温馨提示";
		}
		if (showDialogClose) {
			dialog_close.setVisibility(View.VISIBLE);
		}
		dialog_close.setOnClickListener(new android.view.View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		tv_dialog_title.setText(tilte);
		tv_dialog_title.setTextColor(Color.BLACK);
		if (showTitle) {
			fg_tip.setVisibility(View.VISIBLE);
			findViewById(R.id.line_top).setVisibility(View.VISIBLE);
		}else {
			fg_tip.setVisibility(View.GONE);
			findViewById(R.id.line_top).setVisibility(View.GONE);
		}
		if (mainView != null) {
			RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(-1, -2);
			lp.addRule(RelativeLayout.CENTER_IN_PARENT);
			if (mainViewheight != 0) {
				lp.height = mainViewheight;
				mainViewLayout.addView(mainView, lp);
			} else {
				mainViewLayout.addView(mainView,lp);
			}
		}
		for (int i = 0; i < btnTexts.length; i++) {
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(-1, -2);
			lp.height = CommonMethod.dip2px(mContext, 40);
			lp.weight = 1;
			if (i == 0) {
				button1 = getButton(i);
				CommonMethod.setBackground(button1, getDialogSelectColor(-1));
				buttonlayout.addView(button1, lp);
			} else if (i == 1) {
				button2 = getButton(i);
				CommonMethod.setBackground(button2, getDialogSelectColor(0));
				buttonlayout.addView(button2, lp);
			} else if (i == 2) {
				button3 = getButton(i);
				CommonMethod.setBackground(button3, getDialogSelectColor(1));
				buttonlayout.addView(button3, lp);
			}
			if (i < btnTexts.length - 1) {
				lp = new LinearLayout.LayoutParams(-2, -1);
				lp.width = 2;
				ImageView view = getLineView();
				view.setBackgroundColor(mContext.getResources().getColor(R.color.commondialog_line_color));
				buttonlayout.addView(view, lp);
			}
		}
		if (!TextUtils.isEmpty(subText)) {
			LinearLayout.LayoutParams subLp = new LinearLayout.LayoutParams(-1, -2);
			subLp.height = CommonMethod.dip2px(mContext, 40);
			subLp.weight = 1;
			buttonSub = getButton(subText);
			CommonMethod.setBackground(buttonSub, getDialogSelectColor(2));
			sub_tips_layout.addView(buttonSub, subLp);
		}
	}
	
	@Override
	public void show() {
		super.show();
	}
	
	public void setBtns(DialogListener listener, String... str) {
		this.listener = listener;
		this.btnTexts = str;
	}
	
	/**
	 * 设置主布局
	 */
	public void setMainView(View view) {
		mainView = view;
	}

	public void setMainView(View view, int height) {
		mainView = view;
		mainViewheight = CommonMethod.dip2px(mContext, height);
	}
	
	@SuppressLint("NewApi")
	public Button getButton(int position) {
		final Button button = new Button(mContext);
		button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
		button.setSingleLine();
		if (Build.VERSION.SDK_INT >= 16) {
			button.setBackground(null);
		} else {
			button.setBackgroundDrawable(null);
		}
		button.setId(position);
		button.setGravity(Gravity.CENTER);
		button.setText(btnTexts[position]);
		if (listener != null) {
			button.setOnClickListener(this);
		}
		return button;
	}
	
	@SuppressLint("NewApi")
	public Button getButton(String text) {
		final Button button = new Button(mContext);
		button.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
		if (Build.VERSION.SDK_INT  >=  16) {
			button.setBackground(null);
		} else {
			button.setBackgroundDrawable(null);
		}
		button.setId(3);
		button.setGravity(Gravity.CENTER);
		button.setText(text);
		if (listener != null) {
			button.setOnClickListener(this);
		}
		return button;
	}
	
	
	public ImageView getLineView() {
		ImageView view = new ImageView(mContext);
		return view;
	}
	
	/**
	 * 设置要显示dialog信息
	 * @param msg
	 */
	public void setTextMessage(String msg){
		if (isTextMessageTip) {
			TextView textView = new TextView(mContext);
			textView.setTextColor(Color.BLACK);
			textView.setText(msg);
			if (dialogTextPosition == 0) {
				textView.setGravity(Gravity.CENTER);
			}else if (dialogTextPosition == 1){
				textView.setGravity(Gravity.LEFT);
			}
			textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
			setMainView(textView);
		}
	}
	
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case 0:
				listener.okButtonClick(this);
				break;
			case 1:
				listener.cancelButtonClick(this);
				break;
			case 2:
				listener.otherButtonClick(this);
				break;
			case 3:
				if (onSubTipClickListener != null) {
					onSubTipClickListener.onSubTipClick(this);
				}
				break;
		}
	}
	
	int RADIUS;
	
	public StateListDrawable getDialogSelectColor(int type) {
		if (RADIUS == 0) {
			RADIUS = CommonMethod.dip2px(mContext, 10);
		}
		return addColorDrawable(RADIUS, mContext.getResources().getColor(R.color.white), mContext.getResources().getColor(R.color.bk_grey), type);
	}
	

	// @RADIUS 圆角 @type -1 left 0 center 1right
	public StateListDrawable addColorDrawable(int RADIUS, int normalColor, int pressColor, int type) {
		float[] float1 = null;
		if (type == -1) {
			float1 = new float[] { 0, 0, 0, 0, 0, 0, RADIUS, RADIUS };
		} else if (type == 0) {
			float1 = new float[] { 0, 0, 0, 0, 0, 0, 0, 0 };
		} else if (type == 1) {
			float1 = new float[] { 0, 0, 0, 0, RADIUS, RADIUS, 0, 0 };
		} else {
			float1 = new float[] { 0, 0, 0, 0, RADIUS, RADIUS, RADIUS, RADIUS };
		}
		GradientDrawable gd = new GradientDrawable();
		gd.setCornerRadii(float1);
		gd.setColor(normalColor);
		GradientDrawable gd2 = new GradientDrawable();
		gd2.setCornerRadii(float1);
		gd2.setColor(pressColor);
		StateListDrawable sd = new StateListDrawable();
		sd.addState(new int[] { android.R.attr.state_pressed }, gd2);
		sd.addState(new int[] {}, gd);
		return sd;
	}
	
}
